<?php
	include '../config.php';
	include '../functions.php';
    $db = new db();
    $basepath = getcwd();
	//ini_set('memory_limit', '712M');
	set_time_limit(0);
	$books = $db->get_all_books(FALSE , FALSE, FALSE ,FALSE);
	foreach ($books['list'] as $book){
		
		$ean = trim($book['EAN']);
		if (!empty($ean) && !$book['media']){
	  		$ch = curl_init();
			$url = "http://giotto.ibs.it/copertine/libro.aspx?e=".$ean."&f=l&s=1" ;
			curl_setopt($ch, CURLOPT_URL, $url);
			$fp = fopen("../upload/covers/".$ean.".jpg", 'wb');
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_exec ($ch);
			curl_close ($ch);
			fclose($fp);
			$size = getimagesize("../upload/covers/".$ean.".jpg");
			if ( $size[0] == 117 && $size[1] == 170)
				unlink("../upload/covers/".$ean.".jpg");
			
			else{
				$db->set_media($book['id'], TRUE);
			}
				
		}
	}



?>